package com.company;

/**
 * <一句话功能描述><功能详细描述>
 *
 * @author zhourunjin
 * @version: [版本号，2018-10-16]
 * @see [相关类/方法]
 * @since [产品/模块版本]
 */
public class FinalizeEscapeGC {

    public static FinalizeEscapeGC SAVE_HOOK = null;

    public void idAlive(){
        System.out.println("i am live");
    }

    @Override
    protected void finalize() throws Throwable {
        super.finalize();
        System.out.println("finalize excuted!");
        FinalizeEscapeGC.SAVE_HOOK = this;
    }

    public static void main(String[] args) throws InterruptedException {
        SAVE_HOOK = new FinalizeEscapeGC();

        SAVE_HOOK = null;

        System.gc();

        Thread.sleep(500);

        if (SAVE_HOOK != null){
            SAVE_HOOK.idAlive();
        }else {
            System.out.println("i am died");
        }

        SAVE_HOOK = null;

        System.gc();

        Thread.sleep(500);

        if (SAVE_HOOK != null){
            SAVE_HOOK.idAlive();
        }else {
            System.out.println("i am died");
        }
    }
}
